package 面向对象编程基础.贷款系统;

public class Loan {
    private double monthlyInterestRate;     // 月利率
    private double numberOfMonths;          // 贷款总月数
    private double loanAmount;              // 贷款总额
    public Loan(double monthlyInterestRate, double numberOfMonths, double loanAmount) {
        // 计算月利率，由于年利率用百分比表示，所以要乘以100
        this.monthlyInterestRate = monthlyInterestRate / (100*12);
        this.numberOfMonths = numberOfMonths *12;   // 计算还款月数
        this.loanAmount = loanAmount;
    }
    // 定义方法，计算等额本息还款方式下的月均还款额并返回
    public double getMonthlyPayment() {
        return (loanAmount*monthlyInterestRate*Math.pow(1+monthlyInterestRate,numberOfMonths))
                /(Math.pow(1+monthlyInterestRate,numberOfMonths)-1);
    }
    // 定义方法，实现计算等额本息还款方式下的总还款额并返回
    public double getTotalPayment() {
        return getMonthlyPayment()*numberOfMonths;
    }
}
